<?php
namespace common\records;
use Yii;
use yii\db\ActiveRecord;

class Toy extends ActiveRecord
{
    /**
    ** @return string 返回该AR类关联的数据表名
    **/
    public $imageFile;
    public static function tableName()
    {
        return 'toy';
    }

    public function rules()
    {
        return [
            [['name','level'],'required'],
            [['level',  'status'], 'integer'],
            [['imageFile'], 'file','extensions' => 'png, jpg'],
            [['name', 'timestamp', 'description'], 'string'],
            ];
    }

    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '娃娃名',
            'level' => '稀有程度',
            'photo' => '图片',
            'imageFile' => '图片',
            'description' => '描述',
            'status' => '状态',
            'timestamp' => '添加时间',
            ];
    }

    public function upload()
    {
        //if ($this->validate()) {
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        //} else {
        //    return false;
        //}
    }
    public function beforeSave($insert)
    {
        if (parent::beforeSave($insert)) {
            if ($insert == true) {
                $this->timestamp = date('Y-m-d H:i:s');
            }
            return true;
        }
        return false;
    }
}
